package com.basic.auth.controller;

import com.alibaba.fastjson.JSON;
import com.basic.auth.controller.req.AuthRoleMemberReqForm;
import com.basic.auth.controller.res.AuthRoleMemberResForm;
import com.basic.auth.domain.AuthRoleMemberBo;
import com.basic.auth.service.AuthRoleMemberService;
import com.basic.common.bean.BeanCopyDsUtil;
import com.basic.common.constant.CommonDsConstant;
import com.basic.common.result.WebResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户角色管理controller
 *
 * @author 
 * @version 1.0
 * @Date  15:51
 */
@RestController
@RequestMapping(value = "/roleMember")
//@Api("AuthUserFuncController相关的api")
public class AuthRoleMemberController {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AuthRoleMemberService authRoleMemberService;

//    @ApiOperation(value = "查询用户角色关系", notes = "查询参数")
//    @ApiImplicitParam(name = "AuthRoleReqForm ", value = "查询参数", required = true, dataType = "AuthRoleReqForm ")
    @RequestMapping(value = "/selListBySysIdSubSysId", method = RequestMethod.POST)
    public WebResult selListBySysIdSubSysId(@RequestBody AuthRoleMemberReqForm roleMemberReqForm, BindingResult result) {
        log.info("selListBySysIdSubSysId请求入参:"+ JSON.toJSONString(roleMemberReqForm));
//        log.info("BindingResult请求入参:"+ JSON.toJSONString(result));
//        ApiResult webAuthResult = ApiAuthVerification.getResult(roleMemberReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthRoleMemberBo roleMemberBo = new AuthRoleMemberBo();
        BeanCopyDsUtil.copyProperties(roleMemberReqForm, roleMemberBo);
        List<AuthRoleMemberBo> roleMemberBoList = authRoleMemberService.selListBySysIdSubSysId(roleMemberBo);
        List<AuthRoleMemberResForm> resFormList = modelToResList(roleMemberBoList);
        return WebResult.success(resFormList);
    }

    /**
     * 查询角色成员分页列表
     * 通过POST请求接收参数，返回角色成员的分页列表信息
     *
     * @param roleMemberReqForm 角色成员请求表单，包含查询条件和分页信息
     * @return WebResult 包含查询结果的WebResult对象，结果中包含分页信息
     */
    @RequestMapping(value = "/selRoleMemberPageList", method = RequestMethod.POST)
    public PageInfo selRoleMemberPageList(@RequestBody AuthRoleMemberReqForm roleMemberReqForm) {
        // 将请求表单转换为业务对象
        AuthRoleMemberBo roleMemberBo = new AuthRoleMemberBo();
        // 使用工具类进行属性复制
        BeanCopyDsUtil.copyProperties(roleMemberReqForm, roleMemberBo);

        // 获取分页参数
        Integer pageNum = roleMemberBo.getPageNum();
        Integer pageSize = roleMemberBo.getPageSize();

        // 设置默认分页参数
        if (pageNum == null || pageNum == 0) {
            pageNum = CommonDsConstant.PAGE_NUM;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = CommonDsConstant.PAGE_SIZE;
        }

        // 开始分页
        PageHelper.startPage(pageNum, pageSize);

        // 调用服务方法获取分页列表
        List<AuthRoleMemberBo> roleMemberBoList = authRoleMemberService.selRoleMemberPageList(roleMemberBo);

        // 创建PageInfo对象，用于封装分页信息
        return new PageInfo<>(roleMemberBoList);

//        // 将业务对象列表转换为响应表单列表
//        List<AuthRoleMemberResForm> resFormList = modelToResList(roleMemberBoList);
//
//        // 如果响应表单列表不为空，将其设置为PageInfo的列表
//        if (CollectionUtils.isNotEmpty(resFormList)) {
//            pageInfo.setList(resFormList);
//        }
//
//        // 返回成功结果，携带PageInfo对象
//        return WebResult.success(pageInfo);
    }

    @RequestMapping(value = "/selRoleMemberList", method = RequestMethod.POST)
    public WebResult selRoleMemberList(@RequestBody AuthRoleMemberReqForm roleMemberReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(roleMemberReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthRoleMemberBo roleMemberBo = new AuthRoleMemberBo();
        BeanCopyDsUtil.copyProperties(roleMemberReqForm, roleMemberBo);
        Integer pageNum = roleMemberBo.getPageNum();
        Integer pageSize = roleMemberBo.getPageSize();
        if (pageNum == null || pageNum == 0) {
            pageNum = CommonDsConstant.PAGE_NUM;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = CommonDsConstant.PAGE_SIZE;
        }
        List<AuthRoleMemberBo> roleMemberBoList = authRoleMemberService.selRoleMemberPageList(roleMemberBo);
        List<String> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(roleMemberBoList)) {
            list=  roleMemberBoList.stream().map(AuthRoleMemberBo::getBoId).collect(Collectors.toList());
        }
        return WebResult.success(list);
    }

//    @ApiOperation(value = "删除用户角色关系", notes = "删除参数")
//    @ApiImplicitParam(name = "AuthRoleReqForm ", value = "删除参数", required = true, dataType = "AuthRoleReqForm ")
    @RequestMapping(value = "/delRoleMember", method = RequestMethod.POST)
    public WebResult delRoleMember( @RequestBody AuthRoleMemberReqForm roleMemberReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(roleMemberReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthRoleMemberBo roleMemberBo = new AuthRoleMemberBo();
        BeanCopyDsUtil.copyProperties(roleMemberReqForm, roleMemberBo);
        authRoleMemberService.delRoleMemberByParam(roleMemberBo);
        return WebResult.success();
    }

//    @ApiOperation(value = "为用户设置角色", notes = "添加参数, funcIds有值则添加到用户功能表，没有则不添加")
//    @ApiImplicitParam(name = "AuthRoleReqForm ", value = "添加参数", required = true, dataType = "AuthRoleReqForm ")
    @RequestMapping(value = "/addRoleMember", method = RequestMethod.POST)
    public WebResult addRoleMember( @RequestBody AuthRoleMemberReqForm roleMemberReqForm) {
        AuthRoleMemberBo roleMemberBo = new AuthRoleMemberBo();
        BeanCopyDsUtil.copyProperties(roleMemberReqForm, roleMemberBo);
        authRoleMemberService.addRoleMember(roleMemberBo);
        return WebResult.success();
    }
    /**
     * 批量添加用户角色
     *
     * @param roleMemberReqForm
     * @return void
     * @author xumqc
     * @date 18:40 2024/7/8
     */
    @RequestMapping(value = "/addBatchRoleMember", method = RequestMethod.POST)
    public WebResult addBatchRoleMember( @RequestBody AuthRoleMemberReqForm roleMemberReqForm) {
        AuthRoleMemberBo roleMemberBo = new AuthRoleMemberBo();
        BeanCopyDsUtil.copyProperties(roleMemberReqForm, roleMemberBo);
        return authRoleMemberService.addBatchRoleMember(roleMemberBo);
    }

    /**
     * 配置角色
     * @param roleMemberReqForm
     * @param result
     * @return
     */


//    @ApiOperation(value = "判断用户角色权限", notes = "判断用户角色权限")
//    @ApiImplicitParam(name = "AuthRoleReqForm ", value = "添加参数", required = true, dataType = "AuthRoleReqForm ")
    @RequestMapping(value = "/judgeUserFunc", method = RequestMethod.POST)
    public WebResult judgeUserFuncByRoleAndCode( @RequestBody AuthRoleMemberReqForm roleMemberReqForm, BindingResult result) {
//        ApiResult webAuthResult = ApiAuthVerification.getResult(roleMemberReqForm, result);
//        if (!webAuthResult.isSuccess) {
//            return webAuthResult.getWebResult();
//        }
        AuthRoleMemberBo roleMemberBo = new AuthRoleMemberBo();
        BeanCopyDsUtil.copyProperties(roleMemberReqForm, roleMemberBo);
        return authRoleMemberService.judgeUserFuncByRoleAndCode(roleMemberBo);
    }

    private List<AuthRoleMemberResForm> modelToResList(List<AuthRoleMemberBo> roleFuncList) {
        if (CollectionUtils.isEmpty(roleFuncList)) {
            return null;
        }
        List<AuthRoleMemberResForm> resFormList = new ArrayList<AuthRoleMemberResForm>();
        AuthRoleMemberResForm resForm = null;
        for (AuthRoleMemberBo roleFunc : roleFuncList) {
            resForm = modelToRes(roleFunc);
            resFormList.add(resForm);
        }
        return resFormList;
    }

    private AuthRoleMemberResForm modelToRes(AuthRoleMemberBo roleMemberBo) {
        if (roleMemberBo == null) {
            return null;
        }
        AuthRoleMemberResForm resForm = new AuthRoleMemberResForm();
        BeanUtils.copyProperties(roleMemberBo, resForm);
        return resForm;
    }
    /***
     * 确认关联权限与用户
     * @param roleMemberReqForm
     * @return
     */
    @PostMapping("/confirmAssociation")
    public WebResult confirmAssociation(@RequestBody AuthRoleMemberReqForm roleMemberReqForm){
        AuthRoleMemberBo roleMemberBo = new AuthRoleMemberBo();
        BeanCopyDsUtil.copyProperties(roleMemberReqForm, roleMemberBo);
        return authRoleMemberService.confirmAssociation(roleMemberBo);
    }

    /**
     * 统计角色下属用户数量
     * @param roleCode
     * @return
     */
    @GetMapping("/countUserByRoleCode")
    public WebResult countUserByRoleCode(@RequestParam("roleCode") String roleCode){
        return WebResult.success(authRoleMemberService.countUserByRoleCode(roleCode));
    }

    /**
     * 取消关联权限与用户
     * 即删除本条数据
     */
    @DeleteMapping("/cancelAssociation")
    public WebResult cancelAssociation(@RequestBody AuthRoleMemberReqForm roleMemberReqForm) {
        return authRoleMemberService.cancelAssociation(roleMemberReqForm);
    }

}
